home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / jpl_c.zip / SORTB.C < prev    next >
Text File  |  1986-05-18  |  2KB  |  44 lines

  1. /* 1.0  11-12-84 */
  2. /************************************************************************
  3.  *            Robert C. Tausworthe                *
  4.  *            Jet Propulsion Laboratory            *
  5.  *            Pasadena, CA 91009        1984        *
  6.  ************************************************************************
  7.  *    Bubble-sort subroutine, using the usual shuttle algorithm,
  8.  *    such as found in JPL's MBASIC(tm) user's manual.  It also appears
  9.  *    in
  10.  *
  11.  *    Tausworthe, Robert, STANDARDIZED DEVELOPMENT OF COMPUTER SOFTWARE,
  12.  *    Part I, Methods, Prentice-Hall, Inc., Englewood Cliffs, NJ, 1976.
  13.  *
  14.  *    It is written so the array type is unknown to the algorithm, but is 
  15.  *    known to comp() and exch().  The comparison function comp(i, j) 
  16.  *    must be positive if, and only if, the array elements indexed by
  17.  *    i and j are out of sort.  The function exch(i, j) must exchange 
  18.  *    elements indexed by i and j.
  19.  *
  20.  */
  21.  
  22. #include "defs.h"
  23. #include "stdtyp.h"
  24.  
  25. /************************************************************************/
  26.     VOID
  27. sortB(n, comp, exch)    /* Bubble sort array[0..(n-1)] compared by index
  28.                function comp(i, j), and exchanged by
  29.                index function exch(i, j).            */
  30. /*----------------------------------------------------------------------*/
  31. unsigned n;
  32. int (*comp)();
  33. VOID (*exch)();
  34. {
  35.     FAST unsigned i, j, k, m;
  36.  
  37.     for (i = 0, j = 1; i < n - 1; i++, j++)
  38.         if ((*comp)(i, j) > 0)
  39.         {    (*exch)(i, j);
  40.             for (k = i; k > 0 AND (*comp)(m=k-1, k) > 0; k--)
  41.                 (*exch)(m, k);
  42.         }
  43. }
  44.